package actionForms;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import java.util.regex.Matcher;  
import java.util.regex.Pattern; 

public class addContactValidationForm extends ActionForm {	
	
	private static final long serialVersionUID = 1L;
	private String firstname = null;
	private String lastname = null;
	private String email = null;
	private String street = null;
	private String city = null;
	private String zip = null;
	private String country = null;
	private String numPhoneMobile = null;
	private String numPhoneHome = null;
	private String numPhoneDesktop = null;
	private String [] books = null;
	private String company = null;
	private String SIRET = null;
	
	public String getFirstname() {
		return firstname;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public String getLastname() {
		return lastname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	public String getStreet() {
		return street;
	}

	public void setStreet(String street) {
		this.street = street;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getZip() {
		return zip;
	}

	public void setZip(String zip) {
		this.zip = zip;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public String getNumPhoneHome() {
		return numPhoneHome;
	}

	public void setNumPhoneHome(String numPhoneHome) {
		this.numPhoneHome = numPhoneHome;
	}

	public String getNumPhoneDesktop() {
		return numPhoneDesktop;
	}

	public void setNumPhoneDesktop(String numPhoneDesktop) {
		this.numPhoneDesktop = numPhoneDesktop;
	}

	public String getNumPhoneMobile() {
		return numPhoneMobile;
	}

	public void setNumPhoneMobile(String numPhoneMobile) {
		this.numPhoneMobile = numPhoneMobile;
	}

	public String [] getBooks() {
		return books;
	}

	public void setBooks(String [] books) {
		this.books = books;
	}

	public String getSIRET() {
		return SIRET;
	}

	public void setSIRET(String sIRET) {
		SIRET = sIRET;
	}
	
	public String getCompany() {
		return company;
	}

	public void setCompany(String company) {
		this.company = company;
	}
	
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		firstname = null;
		lastname = null;
		email = null;
		street = null;
		city = null;
		zip = null;
		country = null;
		numPhoneMobile = null;
		numPhoneHome = null;
		numPhoneDesktop = null;
		books = null;
		company = null;
		SIRET = null;
	}
	
	public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();
		
		if (firstname == null || firstname.length() < 1) {
			errors.add("firstname", new ActionMessage("firstname.error.required"));
		}
		
		for (int i = 0; i < firstname.length(); i++) {
            if (!Character.isAlphabetic(firstname.charAt(i))){
            	errors.add("firstname", new ActionMessage("firstname.error.required"));
            	break;
            }
        }

		if (lastname == null || lastname.length() < 1) {
			errors.add("lastname", new ActionMessage("lastname.error.required"));
		}
		
		for (int i = 0; i < lastname.length(); i++) {
            if (!Character.isAlphabetic(lastname.charAt(i))){
            	errors.add("lastname", new ActionMessage("lastname.error.required"));
            	break;
            }
        }

		String expression = "^[\\w\\-]+(\\.[\\w\\-]+)*@([A-Za-z0-9-]+\\.)+[A-Za-z]{2,4}$";
		Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(getEmail());
		if (!matcher.matches())
			errors.add("email", new ActionMessage("email.error.required"));

		if (street == null || street.length() < 1) {
			errors.add("street", new ActionMessage("street.error.required"));
		}

		if (city == null || city.length() < 1) {
			errors.add("city", new ActionMessage("city.error.required"));
		}
		
		for (int i = 0; i < city.length(); i++) {
            if (!Character.isAlphabetic(city.charAt(i))){
            	errors.add("city", new ActionMessage("city.error.required"));
            	break;
            }
        }

		if (zip == null || zip.length() < 1) {
			errors.add("zip", new ActionMessage("zip.error.required"));
		}
		
		for (int i = 0; i < zip.length(); i++) {
            if (!Character.isDigit(zip.charAt(i))){
            	errors.add("zip", new ActionMessage("zip.error.required"));
            	break;
            }
        }

		if (country == null || country.length() < 1) {
			errors.add("country", new ActionMessage("country.error.required"));
		}
		
		if (numPhoneMobile != null || numPhoneMobile.length() > 0) {
			for (int i = 0; i < numPhoneMobile.length(); i++) {
	            if (!Character.isDigit(numPhoneMobile.charAt(i))){
	            	errors.add("numPhoneMobile", new ActionMessage("phone.error"));
	            	break;
	            }
	        }
		}
		
		if (numPhoneHome != null || numPhoneHome.length() > 0) {
			for (int i = 0; i < numPhoneHome.length(); i++) {
	            if (!Character.isDigit(numPhoneHome.charAt(i))){
	            	errors.add("numPhoneHome", new ActionMessage("phone.error"));
	            	break;
	            }
	        }
		}
		
		if (numPhoneDesktop != null || numPhoneDesktop.length() > 0) {
			for (int i = 0; i < numPhoneDesktop.length(); i++) {
	            if (!Character.isDigit(numPhoneDesktop.charAt(i))){
	            	errors.add("numPhoneDesktop", new ActionMessage("phone.error"));
	            	break;
	            }
	        }
		}
		
		if (SIRET != null){
			if(SIRET.length() < 1) {
				errors.add("SIRET", new ActionMessage("siret.error.required"));
			}
			for (int i = 0; i < SIRET.length(); i++) {
	            if (!Character.isDigit(SIRET.charAt(i))){
	            	errors.add("SIRET", new ActionMessage("siret.error.required"));
	            	break;
	            }
	        }
		}
		
		return errors;
	}
} 